home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / customizeCharset.js < prev    next >
Encoding:
JavaScript  |  2005-07-26  |  7.6 KB  |  303 lines

  1. //get prefInt services
  2.  
  3. var availCharsetDict     = [];
  4. var prefInt              = null; //Preferences Interface
  5. var pref_string_title    = "";
  6. var pref_string_content  = "";
  7.  
  8. function Init()
  9. {
  10.   var applicationArea = "";
  11.  
  12.   if ("arguments" in window && window.arguments[0])
  13.     applicationArea = window.arguments[0];
  14.  
  15.   prefInt = Components.classes["@mozilla.org/preferences;1"];
  16.  
  17.   if (prefInt) {
  18.     prefInt = prefInt.getService(Components.interfaces.nsIPref);
  19.  
  20.     if (applicationArea.indexOf("mail") != -1) {
  21.       pref_string_title = "intl.charsetmenu.mailedit";
  22.     } else {
  23.     //default is the browser
  24.       pref_string_title = "intl.charsetmenu.browser.static";
  25.     }
  26.  
  27.     pref_string_content = prefInt.getLocalizedUnicharPref(pref_string_title);
  28.  
  29.     AddRemoveLatin1('add');
  30.   }
  31.  
  32.   LoadAvailableCharSets();
  33.   LoadActiveCharSets();
  34. }
  35.  
  36.  
  37. function readRDFString(aDS,aRes,aProp) 
  38. {
  39.   var n = aDS.GetTarget(aRes, aProp, true);
  40.   if (n)
  41.     return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  42.   else 
  43.     return "";
  44. }
  45.  
  46.  
  47. function LoadAvailableCharSets()
  48. {
  49.   try {
  50.     var available_charsets_listbox = document.getElementById('available_charsets');
  51.     var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
  52.     var kNC_Root = rdf.GetResource("NC:DecodersRoot");
  53.     var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  54.     var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
  55.     var rdfContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
  56.  
  57.     rdfContainer.Init(rdfDataSource, kNC_Root);
  58.     var availableCharsets = rdfContainer.GetElements();
  59.     var charset;
  60.  
  61.     for (var i = 0; i < rdfContainer.GetCount(); i++) {
  62.       charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  63.       availCharsetDict[i] = new Array(2);
  64.       availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
  65.       availCharsetDict[i][1] = charset.Value;
  66.  
  67.       AddListItem(document,
  68.                   available_charsets_listbox,
  69.                   availCharsetDict[i][1],
  70.                   availCharsetDict[i][0]);
  71.     }
  72.   }
  73.   catch (e) {}
  74. }
  75.  
  76.  
  77. function GetCharSetTitle(id)
  78. {
  79.   if (availCharsetDict) {
  80.     for (var j = 0; j < availCharsetDict.length; j++) {
  81.       if (availCharsetDict[j][1] == id) {
  82.         return availCharsetDict[j][0];
  83.       }
  84.     }
  85.   }
  86.   return '';
  87. }
  88.  
  89. function AddRemoveLatin1(action)
  90. {
  91.   var arrayOfPrefs = [];
  92.   arrayOfPrefs = pref_string_content.split(', ');
  93.  
  94.   if (arrayOfPrefs.length > 0) {
  95.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  96.       if (arrayOfPrefs[i] == 'ISO-8859-1') {
  97.         if (action == 'remove') {
  98.           arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
  99.           arrayOfPrefs.length = arrayOfPrefs.length - 1;
  100.         }
  101.  
  102.         pref_string_content = arrayOfPrefs.join(', ');
  103.         return;
  104.       }
  105.     }
  106.  
  107.     if (action == 'add') {
  108.       arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
  109.       pref_string_content = arrayOfPrefs.join(', ');
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. function LoadActiveCharSets()
  116. {
  117.   var active_charsets = document.getElementById('active_charsets');
  118.   var arrayOfPrefs = [];
  119.   var str;
  120.   var tit;
  121.  
  122.   arrayOfPrefs = pref_string_content.split(', ');
  123.  
  124.   if (arrayOfPrefs.length > 0) {
  125.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  126.       str = arrayOfPrefs[i];
  127.       tit = GetCharSetTitle(str);
  128.       if (str && tit)
  129.         AddListItem(document, active_charsets, str, tit);
  130.     }
  131.   }
  132. }
  133.  
  134. function enable_save()
  135. {
  136.   var save_button = document.documentElement.getButton("accept");
  137.   save_button.removeAttribute('disabled');
  138. }
  139.  
  140.  
  141. function update_buttons()
  142. {
  143.   var available_charsets = document.getElementById('available_charsets');
  144.   var active_charsets = document.getElementById('active_charsets');
  145.   var remove_button = document.getElementById('remove_button');
  146.   var add_button = document.getElementById('add_button');
  147.   var up_button = document.getElementById('up_button');
  148.   var down_button = document.getElementById('down_button');
  149.  
  150.   var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
  151.   remove_button.disabled = !activeCharsetSelected;
  152.  
  153.   if (activeCharsetSelected) {
  154.     up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
  155.     down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
  156.   }
  157.   else {
  158.     up_button.disabled = true;
  159.     down_button.disabled = true;
  160.   }
  161.  
  162.   add_button.disabled = (available_charsets.selectedItems.length == 0);
  163. }
  164.  
  165.  
  166.  
  167. function AddAvailableCharset()
  168. {
  169.   var active_charsets = document.getElementById('active_charsets');
  170.   var available_charsets = document.getElementById('available_charsets');
  171.  
  172.   for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
  173.   {
  174.     var selItem =  available_charsets.selectedItems[nodeIndex];
  175.     
  176.     var charsetname  = selItem.label;
  177.     var charsetid = selItem.id;
  178.     var already_active = false;
  179.  
  180.     for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  181.       if (charsetid == item.id) {
  182.         already_active = true;
  183.         break;
  184.       }//if
  185.  
  186.     }//for
  187.  
  188.     if (already_active == false) {
  189.       AddListItem(document, active_charsets, charsetid, charsetname);
  190.     }//if
  191.  
  192.   }//for
  193.  
  194.   available_charsets.clearSelection();
  195.   enable_save();
  196.  
  197. } //AddAvailableCharset
  198.  
  199.  
  200.  
  201. function RemoveActiveCharset()
  202. {
  203.   var listbox = document.getElementById('active_charsets');
  204.   var nextNode = null;
  205.   var numSelected = listbox.selectedItems.length;
  206.   var deleted_all = false;
  207.  
  208.   var numSelectedItems = listbox.selectedItems.length;
  209.   for (count = 0; count < numSelectedItems; count ++) {
  210.     listbox.removeChild(listbox.selectedItems[0]);
  211.   }
  212.  
  213.   listbox.clearSelection();
  214.  
  215.   enable_save();
  216. } //RemoveActiveCharset
  217.  
  218.  
  219.  
  220. function Save()
  221. {
  222.   // Iterate through the 'active charsets  tree to collect the charsets
  223.   // that the user has chosen.
  224.  
  225.   var active_charsets = document.getElementById('active_charsets');
  226.  
  227.   var charsetid    = "";
  228.   var num_charsets = 0;
  229.   var pref_string_content = '';
  230.  
  231.   for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  232.     charsetid = item.id;
  233.  
  234.     if (charsetid.length > 1) {
  235.       num_charsets++;
  236.  
  237.       //separate >1 charsets by commas
  238.       if (num_charsets > 1)
  239.         pref_string_content = pref_string_content + ", " + charsetid;
  240.       else
  241.         pref_string_content = charsetid;
  242.     }
  243.   }
  244.  
  245.   try
  246.   {
  247.     if (prefInt) {
  248.       prefInt.SetCharPref(pref_string_title, pref_string_content);
  249.       window.close();
  250.     }
  251.   }
  252.   catch(ex) {
  253.     confirm('exception' + ex);
  254.   }
  255.   return true;
  256. } //Save
  257.  
  258.  
  259. function MoveUp() {
  260.   var listbox = document.getElementById('active_charsets');
  261.   if (listbox.selectedItems.length == 1) {
  262.     var selected = listbox.selectedItems[0];
  263.     var before = selected.previousSibling
  264.     if (before) {
  265.       listbox.insertBefore(selected, before);
  266.       listbox.selectItem(selected);
  267.       listbox.ensureElementIsVisible(selected);
  268.     }
  269.   }
  270.  
  271.   enable_save();
  272. } //MoveUp
  273.  
  274.  
  275.  
  276. function MoveDown() {
  277.   var listbox = document.getElementById('active_charsets');
  278.   if (listbox.selectedItems.length == 1) {
  279.     var selected = listbox.selectedItems[0];
  280.     if (selected.nextSibling) {
  281.       if (selected.nextSibling.nextSibling)
  282.         listbox.insertBefore(selected, selected.nextSibling.nextSibling);
  283.       else
  284.         selected.parentNode.appendChild(selected);
  285.       listbox.selectItem(selected);
  286.     }
  287.   }
  288.  
  289.   enable_save();
  290. } //MoveDown
  291.  
  292. function AddListItem(doc, listbox, ID, UIstring)
  293. {
  294.   // Create a treerow for the new item
  295.   var item = doc.createElement('listitem');
  296.  
  297.   // Copy over the attributes
  298.   item.setAttribute('label', UIstring);
  299.   item.setAttribute('id', ID);
  300.  
  301.   listbox.appendChild(item);
  302. }
  303.